package com.ruoyi.common.core.mail;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/** 邮件发送服务
 * @Author luowu
 * @Date 2024/8/9 涵 14:49:37
 */
@Component
public class MailSenderService {

    @Resource
    private JavaMailSenderImpl MailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${spring.mail.to}")
    private String to;

    @Value("${spring.mail.subject}")
    private String subject;

    /** 普通邮件
     * @param text    邮箱内容
     */
    public void sendSimpleEmail(String text) {
        //定制纯文本邮信息的 SimpleMailMessage对象
        SimpleMailMessage message = new SimpleMailMessage();
        //设置"发件人"地址
        message.setFrom(from);
        if (StringUtils.isNotBlank(to)) {
            //设置"收件人"地址
            message.setTo(StrUtil.splitToArray(to, ","));
        }
        //设置"邮件标题"
        message.setSubject(subject);
        //设置"邮件发送文本"
        message.setText(text);
        MailSender.send(message);
    }
}
